//
//  NewWSSelectedPayTypeController.m
//  FD
//
//  Created by xiaofang wang on 2018/11/22.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "NewWSPayTypeController.h"
#import "NewWSPayTypeCell.h"
#import "NewPayTypeModel.h"


@interface NewWSPayTypeController ()<UITableViewDelegate,UITableViewDataSource>
/**tv*/
@property (nonatomic, strong) UITableView *tv;
/**1.数据源*/
@property (nonatomic, strong) NSMutableArray *dataArrM;
/**底部确认按钮*/
@property (nonatomic, strong) UIButton *sureBtn;
/**选择支付方式*/
@property (nonatomic) NewPayType pleaseSelectPayType;

@end

@implementation NewWSPayTypeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self initData];
}

- (void)setupUI {
    self.navigationItem.title = [NSString stringWithFormat:@"%@:",NSLocalizedString(Order_PayType, @"支付方式:")];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tv.frame = self.view.bounds;
    [self.view addSubview:self.tv];
    self.tv.tableFooterView = [UIView new];
    
    //确定按钮
    CGFloat y = SCREEN_HEIGHT - FD_TabbarSafeBottomMargin  -40;
    self.sureBtn.frame = CGRectMake(0, y , SCREEN_WIDTH, 40);
    [self.view addSubview:self.sureBtn];
}

- (void)initData {
    NewPayTypeModel *m1 = [NewPayTypeModel new];
    m1.name = NSLocalizedString(L_Pay_Online, @"在线支付");
    m1.payType = 110;
    
    NewPayTypeModel *m2 = [NewPayTypeModel new];
    m2.name = NSLocalizedString(L_Pay_Offline, @"线下支付");
    m2.payType = 111;
    
    [self.dataArrM addObject:m1];
    [self.dataArrM addObject:m2];
}

#pragma mark - getter

- (UITableView *)tv {
    if (!_tv) {
        _tv = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tv.backgroundColor = kMainLightGrayColor;
        //代理
        _tv.delegate = self;
        _tv.dataSource = self;
        
        //注册
        [_tv registerNib:[UINib nibWithNibName:@"NewWSPayTypeCell" bundle:nil] forCellReuseIdentifier:@"NewWSPayTypeCell"];
    }
    return _tv;
}

- (NSMutableArray *)dataArrM {
    if (!_dataArrM) {
        _dataArrM = [NSMutableArray array];
    }
    return _dataArrM;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArrM.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewWSPayTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewWSPayTypeCell"];
    
    NewPayTypeModel *model = self.dataArrM[indexPath.section];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NewPayTypeModel *model = self.dataArrM[indexPath.section];
    [self.dataArrM enumerateObjectsUsingBlock:^(NewPayTypeModel  * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.isSelected = NO;
    }];
    
    model.isSelected = YES; //选中

    
    if (model.payType == 110) { //线上支付
        self.pleaseSelectPayType =  NewPayTypeOnLinePay;
    }else { //线下
        self.pleaseSelectPayType = NewPayTypeOffLine;
    }

    [self.tv reloadData];
    FDLog(@"点击了:%@",model);
}

//间距
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *secHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)];
    secHeaderView.backgroundColor = kMainLightGrayColor;
    
    return secHeaderView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}


#pragma mark - 确定
- (UIButton *)sureBtn {
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_sureBtn addTarget:self action:@selector(clickSureAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [_sureBtn setTitle:@"确认" forState:(UIControlStateNormal)];
        [_sureBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        _sureBtn.backgroundColor = kMainRedColor;
    }
    return _sureBtn;
}

- (void)clickSureAction:(UIButton *)btn {
    FDLog(@"确认");
    
    
    if (!self.pleaseSelectPayType) {
        [JRToast showWithText:NSLocalizedString(Order_SelectPayType, @"请选择支付方式")];
        return;
    }
    
    //回调支付方式  怎么回调
    if (self.selectPayTypeBlock) {
        self.selectPayTypeBlock(self.pleaseSelectPayType);
    }
    
    //直接返回
    [self.navigationController popViewControllerAnimated:YES];
}
@end
